﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="TableAnalyticsClient.cs" company="">
//   
// </copyright>
// <summary>
//   The table analytics client.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace XRay.Library
{
    using System;
    using System.Collections.Generic;
    using System.Threading;
    using System.Threading.Tasks;

    using Microsoft.WindowsAzure.Storage;
    using Microsoft.WindowsAzure.Storage.Shared.Protocol;
    using Microsoft.WindowsAzure.Storage.Table;

    using XRay.Library.Contracts;

    /// <summary>
    ///     The table analytics client.
    /// </summary>
    public class TableAnalyticsClient : AnalyticsClientBase
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="TableAnalyticsClient"/> class.
        /// </summary>
        /// <param name="cloudStorageAccount">
        /// The cloud storage account.
        /// </param>
        /// <param name="location">
        /// The location.
        /// </param>
        public TableAnalyticsClient(CloudStorageAccount cloudStorageAccount, StorageLocation location)
            : base(cloudStorageAccount, StorageService.Blob, location)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="TableAnalyticsClient"/> class.
        /// </summary>
        /// <param name="cloudStorageAccount">
        /// The cloud storage account.
        /// </param>
        public TableAnalyticsClient(CloudStorageAccount cloudStorageAccount)
            : base(cloudStorageAccount, StorageService.Blob, StorageLocation.Primary)
        {
        }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The get capacity.
        /// </summary>
        /// <param name="start">
        /// The start.
        /// </param>
        /// <param name="end">
        /// The end.
        /// </param>
        /// <returns>
        /// The <see cref="IEnumerable"/>.
        /// </returns>
        /// <exception cref="NotImplementedException">
        /// </exception>
        public override IEnumerable<BlobCapacity> GetCapacity(DateTimeOffset start, DateTimeOffset end)
        {
            return this.GetCapacityAsync(start, end, CancellationToken.None).Result;
        }

        /// <summary>
        /// The get capacity async.
        /// </summary>
        /// <param name="start">
        /// The start.
        /// </param>
        /// <param name="end">
        /// The end.
        /// </param>
        /// <param name="cancellationToken">
        /// The cancellation token.
        /// </param>
        /// <returns>
        /// The <see cref="Task"/>.
        /// </returns>
        /// <exception cref="NotImplementedException">
        /// </exception>
        public override Task<IEnumerable<BlobCapacity>> GetCapacityAsync(
            DateTimeOffset start, 
            DateTimeOffset end, 
            CancellationToken cancellationToken)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region Methods

        /// <summary>
        /// The get service properties async.
        /// </summary>
        /// <param name="cancellationToken">
        /// The cancellation token.
        /// </param>
        /// <returns>
        /// The <see cref="Task"/>.
        /// </returns>
        protected override async Task<ServiceProperties> GetServicePropertiesAsync(CancellationToken cancellationToken)
        {
            return
                await
                this.cloudStorageAccount.CreateCloudTableClient()
                    .GetServicePropertiesAsync(
                        new TableRequestOptions { RetryPolicy = this.azureStorageRetryPolicy }, 
                        null, 
                        cancellationToken);
        }

        /// <summary>
        /// The set service properties async.
        /// </summary>
        /// <param name="properties">
        /// The properties.
        /// </param>
        /// <param name="cancellationToken">
        /// The cancellation token.
        /// </param>
        /// <returns>
        /// The <see cref="Task"/>.
        /// </returns>
        protected override async Task SetServicePropertiesAsync(
            ServiceProperties properties, 
            CancellationToken cancellationToken)
        {
            await
                this.cloudStorageAccount.CreateCloudTableClient()
                    .SetServicePropertiesAsync(
                        properties, 
                        new TableRequestOptions { RetryPolicy = this.azureStorageRetryPolicy }, 
                        null, 
                        cancellationToken);
        }

        #endregion
    }
}